# nodemon --watch 03file.py --exec 'python 03file.py'

# f = open('./test-files/test.txt','w') #r读,w写
# f.write('hello world')
# f.close()

# 读取图片文件的二进制数据
import json


f = open('./test-files/star.png','rb') #r读,w写
print('bytes',f.read(10))
# print(f.read(10))
f.close()


# +模式:w+,a+,r+ 

# 读取文本


f = open('./test-files/main.py','r') #r读,w写
while True:
    line = f.readline()
    if not line:
        print('end',len(line))
        break
    print(line,len(line))
# print('line1',f.readline())
# print(f.readline())
# print(f.readline())
# print(f.readline())
# print(f.readline())
# print(f.readline())
# # print(f.read(10))
f.close()


# 拷贝图片
def cpImg(fromPath,toPath):
    with open(fromPath,'rb') as f:
        with open(toPath,'wb') as f2:
            d = f.read(1024)
            while d:
                f2.write(d)
                d = f.read(1024)    
                
cpImg('./test-files/star.png','./test-files/star_copy.png')


#json 序列化
dic = {
'hobby':['football','pingpang','smoke'],
'age':20,
'score':97.6,
'name':'zhangsan'
}
#序列化
jsonStr = json.dumps(dic)
print('json',len(jsonStr),jsonStr)
## 写到文件
with open('./test-files/dic.json','w') as f:
    # f.write(jsonStr)
    json.dump(dic,f)
    

#反序列化
dic2 = json.loads(jsonStr)
print('dic2',type(dic2),dic2 )